package com.sinoglobal.ningxia.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.news.NewsDetailActivity;
import com.sinoglobal.ningxia.beans.NewsBean;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.FlyUtil;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qiwx
 * @createdate 2014-6-16 下午2:01:52
 * @Description: 新闻Adapter
 */
public class NewsAdapter extends BaseAdapter {
    public static final int TYPE_ITEM_IMG_ZERO = 1;
    public static final int TYPE_ITEM_IMG_ONE = 2;
    public static final int TYPE_ITEM_IMG_TWO = 3;
    public static final int TYPE_ITEM_VIDEO = 4;

    List<NewsBean> data;
    private LayoutInflater mInflater;
    FinalBitmap fb;
    int imgWidth;
    int imgHeight;
    Activity activity;
    private boolean isOnClick = true;//是否条目的拦截点击事件 默认为true (在Adapter里处理点击事件,外面的会无响应)

    public NewsAdapter(Activity activity, List<NewsBean> data) {
        this.activity = activity;
        if (data == null) {
            data = new ArrayList<NewsBean>();
        }
        this.data = data;
        mInflater = LayoutInflater.from(activity);
        fb = FinalBitmap.create(activity);
        imgWidth = FlyApplication.widthPixels / 4;
        imgHeight = 3 * imgWidth / 4;
    }

    public NewsAdapter(Activity activity, List<NewsBean> data, boolean isOnClick) {
        this.isOnClick = isOnClick;
        this.activity = activity;
        if (data == null) {
            data = new ArrayList<NewsBean>();
        }
        this.data = data;
        mInflater = LayoutInflater.from(activity);
        fb = FinalBitmap.create(activity);
        imgWidth = FlyApplication.widthPixels / 4;
        imgHeight = 3 * imgWidth / 4;
    }

    public void setData(List<NewsBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addData(List<NewsBean> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        if (data != null) {
            this.data.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImgZeroViewHolder zeroViewHolder;
        ImgOneViewHolder oneViewHolder;
        VideoOneViewHolder videoOneViewHolder;
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_ITEM_IMG_ZERO:
                if (convertView == null) {
                    zeroViewHolder = new ImgZeroViewHolder();
                    convertView = mInflater.inflate(R.layout.item_news_zeroimage, null);
                    zeroViewHolder.tvZeroFlag = (TextView) convertView.findViewById(R.id.tv_item_news_zeroimage_flag);
                    zeroViewHolder.tvZeroTime = (TextView) convertView.findViewById(R.id.tv_item_news_zeroimage_time);
                    zeroViewHolder.tvZeroLookNum = (TextView) convertView.findViewById(R.id.tv_item_news_zeroimage_looknum);
                    zeroViewHolder.tvZeroTitle = (TextView) convertView.findViewById(R.id.tv_item_news_zeroimage_title);
                    convertView.setTag(zeroViewHolder);
                } else {
                    zeroViewHolder = (ImgZeroViewHolder) convertView.getTag();
                }
                NewsBean zeroBean = data.get(position);

                zeroViewHolder.tvZeroTitle.setText(zeroBean.getTitle());
                zeroViewHolder.tvZeroTime.setText(zeroBean.getCreate_time());
                zeroViewHolder.tvZeroFlag.setText(zeroBean.getClass_name());
                zeroViewHolder.tvZeroLookNum.setText("浏览量:" + zeroBean.getL_sums());

                break;
            case TYPE_ITEM_IMG_ONE:
                if (convertView == null) {
                    oneViewHolder = new ImgOneViewHolder();
                    convertView = mInflater.inflate(R.layout.item_news_oneimage, null);
                    oneViewHolder.ivOneImg = (ImageView) convertView.findViewById(R.id.iv_item_news_oneimage_img);
                    oneViewHolder.tvOneFlag = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_flag);
                    oneViewHolder.tvOneTime = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_time);
                    oneViewHolder.tvOneLookNum = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_looknum);
                    oneViewHolder.tvOneTitle = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_title);
                    convertView.setTag(oneViewHolder);
                } else {
                    oneViewHolder = (ImgOneViewHolder) convertView.getTag();
                }
                NewsBean oneBean = data.get(position);

                oneViewHolder.tvOneTitle.setText(oneBean.getTitle());
                oneViewHolder.tvOneTime.setText(oneBean.getCreate_time());
                oneViewHolder.tvOneFlag.setText(oneBean.getClass_name());
                oneViewHolder.tvOneLookNum.setText("浏览量:" + oneBean.getL_sums());
                fb.display(oneViewHolder.ivOneImg, ConnectionUtil.localUrl + oneBean.getImg_url());

                break;
            case TYPE_ITEM_IMG_TWO:
                break;
            case TYPE_ITEM_VIDEO:
                if (convertView == null) {
                    oneViewHolder = new ImgOneViewHolder();
                    convertView = mInflater.inflate(R.layout.item_news_oneimage, null);
                    oneViewHolder.ivOneImg = (ImageView) convertView.findViewById(R.id.iv_item_news_oneimage_img);
                    oneViewHolder.tvOneFlag = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_flag);
                    oneViewHolder.tvOneTime = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_time);
                    oneViewHolder.tvOneLookNum = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_looknum);
                    oneViewHolder.tvOneTitle = (TextView) convertView.findViewById(R.id.tv_item_news_oneimage_title);
                    convertView.setTag(oneViewHolder);
                } else {
                    oneViewHolder = (ImgOneViewHolder) convertView.getTag();
                }
                NewsBean videoBean = data.get(position);

                oneViewHolder.tvOneTitle.setText(videoBean.getTitle());
                oneViewHolder.tvOneTime.setText(videoBean.getCreate_time());
                oneViewHolder.tvOneFlag.setText(videoBean.getClass_name());
                oneViewHolder.tvOneLookNum.setText("浏览量:" + videoBean.getL_sums());
                fb.display(oneViewHolder.ivOneImg, ConnectionUtil.localUrl + videoBean.getImg_url());
                break;
            default:
                break;

        }

        if (isOnClick) {
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int lookNum = Integer.parseInt(data.get(position).getL_sums());
                    data.get(position).setL_sums(lookNum + 1 + "");
                    NewsAdapter.this.notifyDataSetChanged();
                    Intent intent = new Intent(activity, NewsDetailActivity.class);
                    intent.putExtra(NewsBean.class.getCanonicalName(),
                            data.get(position));
                    FlyUtil.intentForward(activity, intent);
                }
            });
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }

    static class ImgZeroViewHolder {
        TextView tvZeroFlag, tvZeroTitle, tvZeroLookNum, tvZeroTime;
    }

    static class ImgOneViewHolder {
        ImageView ivOneImg;
        TextView tvOneFlag, tvOneTitle, tvOneLookNum, tvOneTime;
    }

    static class VideoOneViewHolder {
        ImageView ivVideo;
        TextView tvTitle;
    }
}
